import { message } from 'antd';
import {
  getList,
  markPay,
  getSupplyListData,
  refuse,
  confirm,
  getListStatus,
  getOnceDetail,
  getDetailList,
} from '@/services/once';

export default {
  state: {
    formFieldsValue: {},
    currentPage: 1,
    activeKey: '1',
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailDataList: {
      // 详情列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    valuesArr: [
      'ordRecvId',
      'feeSta',
      'custId',
      'createTime',
      'creName'
    ]
  },
  effects: {
    *markPay({ payload }, { call }) {
      const res = yield call(markPay, payload);
      return res;
    },

    *getList({ payload }, { call, put }) {
      const res = yield call(getList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (res.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message || '获取列表数据：服务端错误');
      }
      return res;
    },

    // 获取接单方列表
    *getSupplyList({ payload }, { call }) {
      const res = yield call(getSupplyListData, payload);
      return res;
    },

    // 获取列表状态
    *getListStatus({ payload }, { call }) {
      const res = yield call(getListStatus, payload);
      return res;
    },

    // 确定
    *getConfirm({ payload }, { call }) {
      const res = yield call(confirm, payload);
      return res;
    },

    // 拒绝
    *getRefuse({ payload }, { call }) {
      const res = yield call(refuse, payload);
      return res;
    },

    // 付费管理一次性详情
    *onceNsDetail({ payload }, { call }) {
      const response = yield call(getOnceDetail, payload);
      return response;
    },

    // 付费管理一次性详情列表
    *onceNsDetailList({ payload }, { call, put }) {
      const res = yield call(getDetailList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'detailList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (res.success) {
        yield put({
          type: 'detailList',
          payload: {
            data: res.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(res.message || '服务端未知错误：获取数据有误');
      }
      return res;
    },
  },
  reducers: {
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 详情列表
    detailList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        detailDataList: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
  },
};
